<?php

require_once(WSB_XTENSION_PATH . 'news/models/DbTable/News/Table.php');

class News_LatestController extends WSB_Xtension_Extension
{

	/**
	 * @return string
	 */
	public function renderForBackend()
	{
		return get_class($this);
	}

	/**
	 * Initialisierung
	 */
	public function init()
	{
		$config = $this->getConfiguration();
		$this->assignToView('detailPageId', empty($config->detailPageId) ? $this->getPageId() : $config->detailPageId);
		$this->assignToView('listPageId', empty($config->listPageId) ? $this->getPageId() : $config->listPageId);
	}

	/**
	 * Index Action
	 */
	public function indexAction()
	{
		$this->assignToView('models', $this->_getModels());
		$this->addContent($this->_view->render('latest.phtml'));
	}

	/**
	 * @return News_Model_DbTable_News_Rowset
	 */
	private function _getModels()
	{
		$config = $this->getConfiguration();
		$limit = is_numeric($config->latestLimit) ? intval($config->latestLimit) : 5 ;
		$table = new News_Model_DbTable_News_Table();
		$select = $table->select();
		$select->where('deleted=?', 0);
		$select->where('instance_id=?', $this->getModuleId());
		$select->order('date DESC');
		$select->limit($limit);
		return $table->fetchAll($select);
	}

}
